/**
 * File: ParallelJobQueue.java
 * Created by: mhaimel
 * Created on: 14 Apr 2009
 * CVS:  $Id: ParallelJobQueue.java,v 1.2 2009/07/07 10:45:43 mhaimel Exp $
 */
package uk.ac.ebi.velvet.exec.impl;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import uk.ac.ebi.velvet.config.VelvetConfig;
import uk.ac.ebi.velvet.exec.ProcessorService;
import uk.ac.ebi.velvet.util.config.ConfigFactory;

/**
 * @author mhaimel
 *
 */
public abstract class ParallelJobQueue <V,T extends Callable<V>,C> extends AbstractJobQueue<V, T,C> {

	private ProcessorService psrv;
	private VelvetConfig config;

	/**
	 * 
	 */
	public ParallelJobQueue() {
		super();
	}
	
	protected ProcessorService getProcessorService() {
		return this.psrv;
	}
	
	public void setProcessorService(ProcessorService srv) {
		this.psrv = srv;
		this.psrv.registerCategory(getQueueName(), getQueueParallel());
	}

	protected abstract String getQueueName();
	protected abstract Integer getQueueParallel();
	
	@Override
	protected Future<V> submitJob(T job) {
		return getProcessorService().process(getQueueName(), job);
	}
	

	protected VelvetConfig getConfig() {
		if(null == config){
			config = ConfigFactory.create(VelvetConfig.class);
		}
		return config;
	}
	
	public void setConfig(VelvetConfig config) {
		this.config = config;
	}
	
	@Override
	protected void doShutdown() {
		this.getProcessorService().shutdown(getQueueName());
	}

	@Override
	protected void forceShutdown() {
		this.getProcessorService().shutdown(getQueueName(), true);
	}
}
